<template>
  <view class="whole">
    <view class="status_bar"></view>

    <u-toast ref="uToast" />

    <view class="title">
      <image
        src="../../static/jiantous.png"
        mode=""
        class="title_image"
        @click="handleBack"
      ></image>
      <text>绑定支付宝</text>

      <view class="title_right" @click="handleDelete">删除</view>
    </view>

    <view class="password_top">
      <view
        class="password"
        style="display: flex; justify-content: flex-start; align-items: center"
      >
        <input
          placeholder="输入真实姓名"
          v-model="name"
          style="font-size: 28rpx; flex: 2"
          placeholder-style="font-size: 28rpx;"
        />
      </view>
      <view
        style="height: 2rpx; background-color: #313535; margin: 0 30rpx"
      ></view>

      <view class="password">
        <input
          placeholder="输入支付宝账号"
          v-model="account_number"
          style="font-size: 28rpx"
          placeholder-style="font-size: 28rpx;"
        />
      </view>
      <view
        style="height: 2rpx; background-color: #313535; margin: 0 30rpx"
      ></view>
    </view>

    <view class="button" @click="Submit"> 确定 </view>
  </view>
</template>

<script>
import api from "../../api/api";
import tip from "@/utils/tip.js";
export default {
  data() {
    return {
      name: "",
      account_number: "",
      id: "",
    };
  },
  onLoad() {
    let alipaylist = JSON.parse(uni.getStorageSync("walletcard"));
    alipaylist.forEach((el) => {
      if (el.zfb_account) {
        this.name = el.real_name;
        this.account_number = el.zfb_account;
        this.id = el.id;
      }
    });
  },
  methods: {
    async Submit() {
      if (this.name == "") {
        this.$refs.uToast.show({
          title: "输入真实姓名",
        });
        return;
      }
      if (this.account_number == "") {
        this.$refs.uToast.show({
          title: "输入支付宝账号",
        });
        return;
      }
      let res = await api.addPayment({
        query: {
          real_name: this.name,
          wallet_id: uni.getStorageSync("walletid"),
          type: 2,
          zfb_account: this.account_number,
          payment_id: this.id,
        },
        method: "POST",
      });
      if (res.code == 0) {
        this.$refs.uToast.show({
          title: "绑定成功",
          type: "success",
        });

        let wallet = await api.userWallet({ method: "get" });
        uni.setStorageSync("walletid", wallet.data.id);
        let walletcard = JSON.stringify(wallet.data.payment_method);
        uni.setStorageSync("walletcard", walletcard);

        setTimeout(() => {
          uni.navigateBack({
            delta: 1,
          });
        }, 500);
      } else {
        this.$refs.uToast.show({
          title: res.msg,
          type: "error",
        });
      }
    },

    handleBack() {
      uni.navigateBack({
        delta: 1,
      });
    },

    async handleDelete() {
      let res = await api.delPayment({
        query: {
          payment_id: this.id,
        },
        method: "get",
      });

      if (res.code == 0) {
        tip.defaults("删除成功");

        uni.navigateBack({
          delta: 1,
        });
      } else {
        tip.defaults(res.msg);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.whole {
  background: $bg-color1;
  min-height: 100vh;
}
.status_bar {
  margin: 0 -30rpx;
  height: var(--status-bar-height);
  width: 750rpx;
}
.title {
  position: relative;
  // width: 68rpx;
  height: 88rpx;
  font-size: 34rpx;
  font-weight: 500;
  text-align: center;
  background: $bg-color1;
  color: #ffffff;
  line-height: 88rpx;
  &_image {
    position: absolute;
    left: 30rpx;
    top: 23rpx;
    width: 42rpx;
    height: 42rpx;
  }
  &_right {
    position: absolute;
    top: 0;
    right: 30rpx;
    height: 88rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: $text-color3;
    line-height: 88rpx;
  }
}
.password_top {
  border: 2rpx solid $bg-color3;
  margin: 0 30rpx;
  background-color: $bg-color3;
  border-radius: 25rpx;
  position: relative;
  top: 50rpx;
  font-size: 28rpx;
}
.password {
  /* display: flex;
		justify-content: space-between;
		align-items: center; */
  color: #ffffff;
  margin: 20rpx 30rpx;
  padding: 5rpx 0;
  /* min-height: 50rpx; */
}
.passwords {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  margin: 20rpx 30rpx;
  min-height: 50rpx;
}
.password_center {
  position: relative;
  top: 50rpx;
  color: #999999;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin: 30rpx;
  font-size: 28rpx;
}
.button {
  position: relative;
  top: 100rpx;
  margin: 30rpx;
  height: 90rpx;
  color: $text-color4;
  background: $button;
  background-repeat: no-repeat;
  background-size: 100%;
  text-align: center;
  line-height: 90rpx;
  font-size: 32rpx;
  border-radius: 16rpx;
}
</style>
